﻿(function (ns, $) {
    ZYCLib.RegNameSpace(ns).Handdler = (function () {

        var Params = {
            PageIndex: 1,
            MBID: 0,
            SupplierID: 0,
            Keyword: "",
            MName: "",
            ShopName: ""
        };

        var _LetterUrl = "";//拼音查找地址 
        var _LetterTarget = "_blank";//拼音.1本页跳转，2弹出页面(默认)

        var initLetterSearch = function (paramsJson, url, targetType) {

            Params.PageIndex = paramsJson.PageIndex;
            Params.MBID = paramsJson.MBID;
            Params.SupplierID = paramsJson.SupplierID;
            Params.Keyword = paramsJson.Keyword;
            Params.MName = paramsJson.MName;
            Params.ShopName = paramsJson.ShopName;

            if (Params.MBID > 0) {
                appendDom(searchType.PZ.Name, searchType.PZ.DomID, Params.MBID, Params.MName);
            }
            if (Params.SupplierID > 0) {
                appendDom(searchType.SJ.Name, searchType.SJ.DomID, Params.SupplierID, Params.ShopName);
            }
            if (Params.Keyword != null && Params.Keyword.length > 0) {
                $("#keyword").val(Params.Keyword);
                appendDom(searchType.PM.Name, searchType.PM.DomID, 0, Params.Keyword);

            }

            if (_LetterUrl == "" && url != null && url != "") {
                _LetterUrl = url;
            }

            if (targetType == 1) {
                _LetterTarget = "_self";
            }
        };

        //点击字母
        var getCodexLetter = function (letter) {
            ZYCLib.Web.JsonAjax.PostData("ElecSupplierPriceService/GetCodexLetter",
                { letter: letter },
                function (ret) {
                    var letterHtml = "";
                    for (var i = 0; i < ret.ListLetter.length; i++) {
                        var eachLetter = ret.ListLetter[i];
                        if (i == 0) {
                            letterHtml += " <a href=\"javascript:void(0);\" sign='subletter' onclick=\"ZYCLib.ESP.Handdler.GetCodexNameByLetter('" + eachLetter + "');\"  class=\"selected\" >" + eachLetter + "</a> ";
                        } else {
                            letterHtml += " <a href=\"javascript:void(0);\" sign='subletter' onclick=\"ZYCLib.ESP.Handdler.GetCodexNameByLetter('" + eachLetter + "');\">" + eachLetter + "</a> ";
                        }
                    }
                    $("#divMCodexSubLetters").html(letterHtml);
                },
                function () {
                });
        };

        //点击字母组合
        var getCodexNameByLetter = function (letter) {
            ZYCLib.Web.JsonAjax.PostData("ElecSupplierPriceService/GetMCodexDataByPinyin",
                { letter: letter, url: _LetterUrl },
                function (ret) {
                    var codexHtml = "";
                    for (var i = 0; i < ret.MCodexList.length; i++) {
                        var eachCodex = ret.MCodexList[i];
                        if (i == 0) {
                            codexHtml += "<a href=\"javascript:void(0);\" sign='codexname' codexid=\"" + eachCodex.MBID + "\" onclick=\"ZYCLib.ESP.Handdler.SearchObjClick('" + searchType.PZ.Name + "','" + searchType.PZ.DomID + "'," + eachCodex.MBID + ",'" + eachCodex.MName + "')\" class=\"selected\">" + eachCodex.MName + "</a>";
                        } else {
                            codexHtml += "<a href=\"javascript:void(0);\" sign='codexname' codexid=\"" + eachCodex.MBID + "\" onclick=\"ZYCLib.ESP.Handdler.SearchObjClick('" + searchType.PZ.Name + "','" + searchType.PZ.DomID + "'," + eachCodex.MBID + ",'" + eachCodex.MName + "')\" >" + eachCodex.MName + "</a>";
                        }
                    }
                    $("#divPinyinMCodex").html(codexHtml);
                },
            function () {
                $("#divMCodexSubLetters").find(".selected").attr("class", "");
                $.each($("#divMCodexSubLetters").children(), function (i, item) {
                    if (item.innerHTML == letter) {
                        $(item).attr("class", "selected");
                    }
                });
                if ($("#divMCodexSubLetters").find(".selected").length == 0) {
                    $($("#divMCodexSubLetters").children()[0]).attr("class", "selected");
                }
            });
        };

        var searchType = {
            PZ: { Name: '品种', DomID: "#pztag" },
            SJ: { Name: '商家', DomID: "#sjtag" },
            PM: { Name: '关键字', DomID: "#keytag" }
        };

        //搜索btn
        $(function () {
            $("#sub_search").bind("click", function () {
                Params.Keyword = encodeURI($.trim($("#keyword").val()));
                //if (Params.Keyword != null && Params.Keyword.length == 0) {
                //    ZYCLib.UI.MessageBox.Alert("请输入关键字!");
                //    return;
                //}
                searchObjClick(searchType.PM.Name, searchType.PM.DomID, "0", Params.Keyword);
            });
        });

        var appendDom = function (domName, domID, objId, objName) {
            var dom = "<em>" + domName + "：</em><i>" + objName + "</i><a href=\"javascript:void 0;\" onclick=\"ZYCLib.ESP.Handdler.DelSelectedMName('" + domID + "'," + objId + ",'" + objName + "')\"><s></s></a>";
            //if (domID != searchType.PM.DomID) {
                $(domID).removeClass("none");
                $(domID).html(dom);
            //}
        };

        //点击字母对应品种或商家进行搜索
        var searchObjClick = function (domName, domID, objId, objName) {
            appendDom(domName, domID, objId, objName);

            $("#divMCodexSubLetters").css("display", "none");
            $("#divPinyinMCodex").css("display", "none");
            $(".pyTop").children(".selected").toggleClass("selected");

            switch (domID) {
                case searchType.PZ.DomID:
                    Params.MBID = objId;
                    Params.MName = objName;
                    break;
                case searchType.SJ.DomID:
                    Params.SupplierID = objId;
                    Params.ShopName = objName;
                    break;
            }
            Params.PageIndex = 1;
            freshHerf();
        };

        //删除已选择的objName
        var delSelectedMName = function (domID, objId, objName) {
            $(domID).addClass("none");
            $(domID).html("");

            switch (domID) {
                case searchType.PZ.DomID:
                    Params.MBID = 0;
                    Params.MName = "";
                    break;
                case searchType.SJ.DomID:
                    Params.SupplierID = 0;
                    Params.ShopName = "";
                    break;
                case searchType.PM.DomID:
                    Params.Keyword = "";
                    break;
            }
            Params.PageIndex = 1;
            freshHerf();
        };

        //URL
        var freshHerf = function () {

            var href = "/jiage/ds" + Params.SupplierID + "-" + Params.MBID + "-" + Params.PageIndex + ".html";
            if (Params.Keyword != "") {
                href += "?keyword=" + Params.Keyword;
            }
            window.location = href;
        };

        var getSubLetter = function (obj, letter) {

            $(obj).siblings().removeAttr("class");
            $(obj).attr("class", "selected");

            $("#divMCodexSubLetters").css("display", "block");
            $("#divPinyinMCodex").css("display", "block");

            getCodexLetter(letter);
            getCodexNameByLetter(letter);
        };

        return {
            InitLetterSearch: initLetterSearch,
            GetCodexNameByLetter: getCodexNameByLetter,
            SearchType: searchType,
            SearchObjClick: searchObjClick,
            GetSubLetter: getSubLetter,
            DelSelectedMName: delSelectedMName
        };
    })();
})("ESP", jQuery);
